/**
* ...
* @author Exey Panteleev
*/
package {
	import flash.display.DisplayObject;
	import org.papervision3d.core.geom.Lines3D;
	import org.papervision3d.core.geom.renderables.Line3D;
	import org.papervision3d.materials.special.LineMaterial;
	import org.papervision3d.objects.DisplayObject3D;
	import caurina.transitions.Tweener;
	import gs.TweenLite;
	import fl.motion.easing.Cubic;
	
public class Plant extends DisplayObject3D {
	
	public var lineMat:LineMaterial;
	public var containerForDrawing:Lines3D; 
	public var marker:DisplayObject3D = new DisplayObject3D();
	public var thisLSystem:LSystem;
	public var time:Number;
	public var growingCount:uint = 0;
	
	
	public function Plant( LSys:LSystem ) {
		createPlant(LSys);
	}
	
	public function createPlant(LSys:LSystem):void {
		lineMat = new LineMaterial(0x6ad300);
		containerForDrawing = new Lines3D(lineMat); 
		addChild(containerForDrawing);
		thisLSystem = LSys;
	}
	
	public function draw() {
		if (growingCount < thisLSystem.branchArray.length ) { // check 
			if (thisLSystem.trunk.length > 0 ) {
					for ( var n:uint = 0; n < thisLSystem.trunk.length; n++)
					{
						growTrunkBranch(n);
					}					
			}
			
				for ( var i:uint = 0; i < thisLSystem.branchGroups.length; i++) {
					for ( var j:uint = 0; j < thisLSystem.branchGroups[i].length; j++)
					{
						//trace("draw", i, j)
						growGroupBranch(i, j);
						growingCount++
					}
				}
		}
	}
	
	public function growGroupBranch(groupIndex:uint, index:uint) {
		var delay:Number = 0;
		for (var i:uint = 0; i < index; i++ )
		{
			delay += thisLSystem.branchGroups[groupIndex][i].growingTime;
		}
		//trace("growGroupBranch ", groupIndex, index, delay, thisLSystem.branchGroups[groupIndex][i].growingTime)
		var time4grow:Number = thisLSystem.branchGroups[groupIndex][index].growingTime;
		thisLSystem.branchGroups[groupIndex][index].line = new Line3D(containerForDrawing, lineMat, thisLSystem.size, thisLSystem.branchGroups[groupIndex][index].lineVertex1, thisLSystem.branchGroups[groupIndex][index].lineVertex2);
		containerForDrawing.addLine(thisLSystem.branchGroups[groupIndex][index].line);
		TweenLite.to(thisLSystem.branchGroups[groupIndex][index].lineVertex2, time4grow, {x:thisLSystem.branchGroups[groupIndex][index].endX, 
																						y:thisLSystem.branchGroups[groupIndex][index].endY, 
																						z:thisLSystem.branchGroups[groupIndex][index].endZ, 
																						delay: delay,
																						ease:Cubic.easeInOut,
																						onComplete:thisLSystem.branchGroups[groupIndex][index].finishGrowing});
	}
	
	public function growTrunkBranch(index:uint) {
		if(thisLSystem.trunk.length > 0){
			thisLSystem.trunk[index].growing = true;
			thisLSystem.trunk[index].line = new Line3D(containerForDrawing, lineMat, thisLSystem.size, thisLSystem.trunk[index].lineVertex1, thisLSystem.trunk[index].lineVertex2);
			containerForDrawing.addLine(thisLSystem.trunk[index].line);
			TweenLite.to(thisLSystem.trunk[index].lineVertex2, 0.05, {x:thisLSystem.trunk[index].endX, 
																								y:thisLSystem.trunk[index].endY, 
																								z:thisLSystem.trunk[index].endZ, 																								
																								ease:Cubic.easeInOut,
																								onComplete:thisLSystem.trunk[index].finishGrowing});		
		}
	}
	
	private function randRange(min:Number, max:Number):Number {
		var randomNum:Number = (Math.random() * (max - min + 1)) + min;
		return randomNum;
	}//randRange without floor
}
}